package com.steven.im.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class IMTimeUtils {
    private static IMTimeUtils imTimeUtils = null;

    public static IMTimeUtils getInstance() {
        if (imTimeUtils == null) {
            synchronized (IMTimeUtils.class) {
                if (imTimeUtils == null) {
                    try {
                        imTimeUtils = new IMTimeUtils();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return imTimeUtils;

    }

    //判断时间并返回对应的格式
    public String getTime(String time) {

        try {
            if (isToday(time)) {
                return todayTime(time);
            } else if (isYesterday(time)) {
                return "昨天";
            } else {
                return agoTime(time);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "null";
        }
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @return true今天 false不是
     */
    private boolean isToday(String time) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(timesToString(time));
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @return true今天 false不是
     */
    private boolean isYesterday(String time) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(timesToString(time));
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    @SuppressLint("SimpleDateFormat")
    private String timesToString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date.setTime(Long.parseLong(time));
        return sdf.format(date);
    }

    private SimpleDateFormat getDateFormat() {
        ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<>();
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    //如果是今天
    @SuppressLint("SimpleDateFormat")
    private String todayTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        Date date = new Date();
        date.setTime(Long.parseLong(time));
        return sdf.format(date);
    }

    //既不是昨天也不是今天的
    @SuppressLint("SimpleDateFormat")
    private String agoTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date.setTime(Long.parseLong(time));
        return sdf.format(date);
    }
}
